﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Benefit.Models.View
{
    /// <summary>
    /// 交易员日交易Tick数据
    /// </summary>
    public class OperatorDayTick
    {
        public int Id { get; set; }
        /// <summary>
        /// 账户
        /// </summary>
        public int OperatorId { get; set; }

        public virtual Sys.Operator Operator { get; set; }

        public int TradeHistoryId { get; set; }

        public virtual Sys.TradeHistory TradeHistory { get; set; }

        /// <summary>
        /// 是否亏损
        /// </summary>
        public bool Win { get; set; }
        /// <summary>
        /// 发生TICK值
        /// </summary>
        public int Tick { get; set; }
        /// <summary>
        /// 次数
        /// </summary>
        public int Count { get; set; }
        /// <summary>
        /// 合约编号
        /// </summary>
        public int InstrumentId { get; set; }
        /// <summary>
        /// 多空方向
        /// </summary>
        public int Direction { get; set; }
        ///// <summary>
        ///// 得到操作员日盈利或亏损的Tick数量
        ///// </summary>
        ///// <param name="operatorid"></param>
        ///// <param name="tradeHistoryId"></param>
        ///// <param name="win"></param>
        ///// <returns></returns>
        //public List<OperatorDayTick> GetOperatorDayWinOrLostTicks(int operatorid, int tradeHistoryId, bool win, DB.DBManager db)
        //{
        //    var query = db.OperatorDayTick.Where(a => a.OperatorId == operatorid).Where(a => a.TradeHistoryId == tradeHistoryId).Where(a => a.Win == win).OrderBy(a => a.Tick);
        //    List<OperatorDayTick> returnValues = new List<OperatorDayTick>();

        //    for (int i = 1; i < 11; i++)
        //    {
        //        OperatorDayTick tick = new OperatorDayTick();
        //        tick.Tick = i;
        //        var query1 = query.Where(a => a.Tick == tick.Tick);
        //        if (query1 == null || query1.Count() == 0)
        //        {
        //            tick.Count = 0;
        //        }
        //        else
        //        {
        //            tick.Count = query1.Sum(a => a.Count);
        //        }

        //        returnValues.Add(tick);
        //        tick = null;
        //    }
        //    OperatorDayTick tick2 = new OperatorDayTick();
        //    tick2.Tick = 11;
        //    var query2 = query.Where(a => a.Tick > 10);
        //    if (query2.Count() > 0)
        //    {
        //        tick2.Count = query2.Sum(a => a.Count);
        //    }
        //    else
        //    {
        //        tick2.Count = 0;
        //    }

        //    returnValues.Add(tick2);
        //    return returnValues;
        //}

        //public void InitOperatorDayTick(int tradeHistoryId, DB.DBManager db, bool win)
        //{
        //    List<OperatorDayTick> ticks = new List<OperatorDayTick>();
        //    var operators = db.Operators.ToList();
        //    foreach (Sys.Operator op in operators)
        //    {
        //        List<Sys.Account> accounts = new Sys.DayOperatorAccount().GetDayOperatorAccounts(new Sys.TradeHistory().GetDateTimeFromTradeHistoryId(tradeHistoryId, db), op.Id, db);
        //        foreach (Sys.Account account in accounts)
        //        {
        //            var query = db.DayWinOrLoseTick.Where(a => a.AccountId == account.Id).Where(a => a.TradeHistoryId == tradeHistoryId).Where(a => a.Win == win).ToList();
        //            foreach (Data.Day.DayWinOrLoseTick tick in query)
        //            {
        //                var queryExist = from t in ticks where t.Tick == tick.Tick && t.Direction == tick.Direction select t;
        //                if (queryExist.Count() > 0)//已存在这个Tick值的记录
        //                {
        //                    queryExist.First().Count = queryExist.First().Count + tick.Count;
        //                }
        //                else
        //                {
        //                    OperatorDayTick _tick = new OperatorDayTick();
        //                    _tick.Tick = tick.Tick;
        //                    _tick.Count = tick.Count;
        //                    _tick.OperatorId = op.Id;
        //                    _tick.TradeHistoryId = tradeHistoryId;
        //                    _tick.Win = win;
        //                    _tick.Direction = tick.Direction;
        //                    ticks.Add(_tick);
        //                }
        //            }
        //        }
        //    }
        //    foreach (OperatorDayTick c in ticks)
        //    {
        //        db.OperatorDayTick.Add(c);
        //    }
        //    db.SaveChanges();
        //}

        //public void Delete(int tradeHistoryId, DB.DBManager db)
        //{
        //    var query = db.OperatorDayTick.Where(a => a.TradeHistoryId == tradeHistoryId);
        //    foreach (OperatorDayTick t in query)
        //    {
        //        db.OperatorDayTick.Remove(t);
        //    }
        //    db.SaveChanges();
        //}
    }
}
